package servlet.request.exercises;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-07-16
 * Time:18:14
 */
@WebServlet(name = "RegisterServlet", urlPatterns = "/registerServlet")
public class RegisterServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String pwd1 = req.getParameter("pwd1");
        String pwd2 = req.getParameter("pwd2");
        String gender = req.getParameter("gender");
        String[] sports = req.getParameterValues("sport");
        String city = req.getParameter("city");
        String info = req.getParameter("info");

        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if (pwd1.equals(pwd2)) {
            writer.println("用户名:" + username + "<br/>");
            writer.println("密码:" + pwd1 + "<br/>");
            writer.println("性别:" + gender + "<br/>");
            for (String sport : sports) {
                writer.println("运动:" + sport + "<br/>");
            }
            writer.println("城市:" + city + "<br/>");
            writer.println("个人介绍:" + info + "<br/>");
        } else {
            writer.println("确认密码错误<br/>");
        }
        writer.flush();
        writer.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
